package com.cdut.hwadeefsc.controller;

import com.cdut.hwadeefsc.entity.comments;
import com.cdut.hwadeefsc.service.commentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/students")
public class commentsController {

    @Autowired
    private commentsService commentsService;

    // 查看学生评语
    @GetMapping("/{studentId}/comments")
    public ResponseEntity<List<comments>> getStudentComments(
            @PathVariable Long studentId,
            @RequestParam(required = false) String semester) {
        try {
            List<comments> comments;
            if (semester != null && !semester.isEmpty()) {
                comments = commentsService.getStudentCommentsBySemester(studentId, semester);
            } else {
                comments = commentsService.getStudentComments(studentId);
            }
            return ResponseEntity.ok(comments);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 填写评语（班主任权限）
    @PostMapping("/{studentId}/comments")
    public ResponseEntity<?> addComment(@PathVariable Long studentId, @RequestBody comments comment) {
        try {
            comment.setStudentId(studentId);
            boolean success = commentsService.addComment(comment);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
